home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: in1.uu.net!world!mv!usenet
- From: ENGR@GSSI.MV.COM (Michael Furman)
- Subject: Counting of 1 bits (was: Re: quick decision: is n a power of 2?)
- Message-ID: <DLqtBM.87w@mv.mv.com>
- Mime-Version: 1.0
- Organization: GSSI
- Date: Thu, 25 Jan 1996 15:21:21 GMT
- References: <AAQgE1nWVS@csg.spb.su> <822516654snz@genesis.demon.co.uk>
- X-Newsreader: WinVN 0.93.10
- X-Nntp-Posting-Host: gssi.mv.com
-
- In article <822516654snz@genesis.demon.co.uk>, fred@genesis.demon.co.uk
- says...
- >
- >In article <AAQgE1nWVS@csg.spb.su>
- > alex@csg.spb.su "Alexander N. Pryguichev" writes:
- >
- >>There is well known solution:
- >>
- >>Boolean IsPowerOfTwo(unsigned long x)
- >>{ long y;
- >> y = x & 0x55555555; y += (x >> 1) & 0x55555555;
- >> x = y & 0x33333333; x += (y >> 2) & 0x33333333;
- >> y = x & 0x0F0F0F0F; y += (x >> 4) & 0x0F0F0F0F;
- >> x = y & 0x00FF00FF; x += (y >> 8) & 0x00FF00FF;
- >> y = x & 0x0000FFFF; y += (x >>16) & 0x0000FFFF;
- >
- >That is a well known method for counting the number of 1 bits in a 32 bit
- >value.
-
- Could you please provide some reference for this method. I am jus curious
- who was 1-st inventor of that method. I know that it was invented in Russia
- by M. Bongard or A. Brudno (I am not sure). And I learned it in about 1966.
-
- And by the way, is a simillar metod for transponing a bynary matrix also
- well known?
-
- >--
- >-----------------------------------------
- >Lawrence Kirby | fred@genesis.demon.co.uk
- >Wilts, England | 70734.126@compuserve.com
- >-----------------------------------------
-
- --
- <<<<<<<< This is a copy of post to the newsgroup >>>>>>>>
- ---------------------------------------------------------------
- Michael Furman, (603)893-1109
- Geophysical Survey Systems, Inc. fax:(603)889-3984
- 13 Klein Drive - P.O. Box 97 engr@gssi.mv.com
- North Salem, NH 03073-0097 71543.1334@compuserve.com
- ---------------------------------------------------------------
-
-